package br.edu.ufcg.dsc.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import br.edu.ufcg.dsc.services.HolidayService;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

/**
 * Esta entidade representa um usuario do sistema.
 * 
 * @author Diogo Anderson
 * @author Felipe Sampaio
 * @author Vinicius Cavalcanti
 */
@Entity
@Table(name="LIBUSER")
public class User {
        
        private String name, type, rg, cpf, address, city, province, complement, email, login, password;
        
        private List<Library> libraries;

        private Library library;
        
        private List<Reservation> reservations;

		private List<Loan> loans;

        public User() {
			reservations = new ArrayList<Reservation>();
			loans = new ArrayList<Loan>();
			libraries = new ArrayList<Library>();
		}
        
        
        @ManyToMany
    	@JoinColumn(name="LIBRARY_ID")
        public List<Library> getLibraries() {
			return libraries;
		}

		public void setLibraries(List<Library> libraries) {
			this.libraries = libraries;
		}
		
		@ManyToOne
		@JoinColumn(name="LIBRARIAN_FOR")
        public Library getLibrary() {
			return library;
		}

		public void setLibrary(Library library) {
			this.library = library;
			this.libraries.add(library);
			System.out.println("ADICIONOU A BIBLIOTECA");
		}


		@Column(name="TYPE")
        public String getType() {
			return type;
		}
        
		public void setType(String type) {
			this.type = type;
		}
		
		@Column(name="USER_NAME")
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        
        @Column(name="RG")
        public String getRg() {
                return rg;
        }
        
        public void setRg(String rg) {
                this.rg = rg;
        }
        
        @Column(name="CPF")
        public String getCpf() {
                return cpf;
        }
        public void setCpf(String cpf) {
                this.cpf = cpf;
        }
        
        @Column(name="ADDRESS")
        public String getAddress() {
                return address;
        }
        public void setAddress(String address) {
                this.address = address;
        }
        
        @Column(name="CITY")
        public String getCity() {
                return city;
        }
        public void setCity(String city) {
                this.city = city;
        }
        
        @Column(name="PROVINCE")
        public String getProvince() {
                return province;
        }
        public void setProvince(String province) {
                this.province = province;
        }
        
        @Column(name="COMPLEMENT")
        public String getComplement() {
                return complement;
        }
        public void setComplement(String complement) {
                this.complement = complement;
        }
        
        @Column(name="EMAIL")
        public String getEmail() {
                return email;
        }
        public void setEmail(String email) {
                this.email = email;
        }
        
        @Id
        @Column(name="USER_LOGIN", unique=true, nullable=false)
        @RequiredStringValidator(fieldName="login",message="default message", key="user.login.required", trim=true)
        public String getLogin() {
                return login;
        }
        public void setLogin(String login) {
                this.login = login;
        }
        
        @Column(name="USER_PASSWORD", nullable=false)
        public String getPassword() {
                return password;
        }
        
        public void setPassword(String password) {
                this.password = password;
        }
        
        public double calculateFine() {
        	double result = 0.0;
        	for (Loan l : getLoans()) {
        		if (!l.isOpen())
        			continue;
        		
        		int days = 0;
        		
        		if (l.getDevolutionDate() == null)
        			days = HolidayService.countBusinessDaysSince(l.getStartDate());
        		else
        			days = HolidayService.businessDaysBetweenDates(l.getStartDate(), l.getDevolutionDate()) - 1;
        		
        		days -= calculateQuantityOfDays(l.getUnit().getLibrary());
        		
        		if (days > 0)
        			result += days * l.getUnit().getLibrary().getFine();
        	}
        	
        	return result;
        }
        
        public int calculateQuantityOfDays(Library l) {
        	if (type.equals("STUDENT"))
        		return l.getDaysStudent();
        	else
        		return l.getDaysTeacher();
        }
        
        @OneToMany(mappedBy="user")
    	public List<Reservation> getReservations() {
    		return reservations;
    	}

    	public void setReservations(List<Reservation> reservations) {
    		this.reservations = reservations;
    	}
    	
    	@OneToMany(mappedBy="user")
    	public List<Loan> getLoans() {
    		return loans;
    	}

    	public void setLoans(List<Loan> loans) {
    		this.loans = loans;
    	}

}
